using Moq;
using Myotragus.Data.Criteria;
using Myotragus.Data.Tests.Domain;
using NHibernate;
using NHibernate.Criterion;
using NUnit.Framework;

namespace Myotragus.Data.Tests.Tests.Criteria
{
	[TestFixture]
	public class QueryBuilderFixtures
	{
		[Test]
		public void Count()
		{
			var sessionFactory = new Mock<ISessionFactoryAdapter>();
			var criteria = new Mock<ICriteria>();
			var rowCount = Projections.RowCount();
			const int rowCountResult = 101;

			criteria.Setup(m => m.SetProjection(It.Is<IProjection>(
				p => p.ToString() == rowCount.ToString())));
			criteria.Setup(m => m.UniqueResult<int>()).Returns(rowCountResult);

			var builder = new QueryBuilder(sessionFactory.Object);

			Assert.AreEqual(rowCountResult, builder.Count(criteria.Object));

			criteria.VerifyAll();
		}

		[Test]
		public void CreateQuery()
		{
			var sessionFactory = new Mock<ISessionFactoryAdapter>();
			var criteria = new Mock<ICriteria>();
			var query = new CriteriaQuery<ITestEntity>(criteria.Object);

			var builder = new QueryBuilder(sessionFactory.Object);

			Assert.AreEqual(query, builder.CreateQuery<ITestEntity>(criteria.Object));
			Assert.AreNotEqual(query, builder.CreateQuery<ITestEntity2>(criteria.Object));
		}

		[Test]
		public void CreateCriteria()
		{
			var sessionFactory = new Mock<ISessionFactoryAdapter>();
			var criteria = new Mock<ICriteria>();
			var session = new Mock<ISession>();

			sessionFactory.Setup(m => m.OpenSession()).Returns(session.Object);
			session.Setup(m => m.CreateCriteria(typeof(ITestEntity))).Returns(criteria.Object);

			var builder = new QueryBuilder(sessionFactory.Object);

			builder.CreateCriteria(typeof(ITestEntity));

			sessionFactory.VerifyAll();
			session.VerifyAll();
		}
	}
}